package vista;

import javax.swing.*;

import controle.*;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.List;
import java.awt.Rectangle;
import java.awt.Panel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class GUIcarregarjogo {

	JFrame janela;
	Main mainCarregaJogo;
	DefaultListModel listDefault;
	TextArea textArea;
	JList list;
	Timer t;
	
	public GUIcarregarjogo(Main m) {
		
		mainCarregaJogo = m;
		mainCarregaJogo.cenario = m.cenario;
		mainCarregaJogo.game = m.game;
		mainCarregaJogo.aberto = m.aberto;
		
		janela = new JFrame();
		BorderLayout borderLayout = (BorderLayout) janela.getContentPane().getLayout();
		janela.setTitle("GuitaRPG");
		janela.setVisible(true);
		janela.setExtendedState(JFrame.MAXIMIZED_BOTH);
		janela.setResizable(false);
		janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		textArea = new TextArea();
		textArea.setEditable(false);
		janela.getContentPane().add(textArea, BorderLayout.EAST);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.ORANGE);
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setVgap(100);
		flowLayout.setHgap(50);
		janela.getContentPane().add(panel, BorderLayout.CENTER);
		
		list = new JList();
		listDefault = new DefaultListModel();
		//DefaultListSelectionModel listDefaultSelection = new DefaultListSelectionModel(); 
		
		//listDefault.addElement("2");
		
		addList();
	
		//listDefaultSelection.addListSelectionListener(blabla);
		
		list.setModel(listDefault);
		//list.setSelectionModel(listDefaultSelection);
		
		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setPreferredSize(new Dimension(300, 300));
		panel.add(scrollPane);
		
		JButton btnNewButton = new JButton("OK");
		btnNewButton.addActionListener(new OuvinteBotaoOK());
		btnNewButton.setPreferredSize(new Dimension(100, 100));
		panel.add(btnNewButton);
		
		t = new Timer(50, new OuvinteEdit());
		t.start();
		
		
	}
	
	void addList () {
		
		File diretorio = new File("D:\\Saves\\");  
		String [] arquivos = diretorio.list();  
		for(int i=0; i<arquivos.length; i++) {  
			listDefault.addElement(arquivos[i].toString());
		}  
	}
	
	void setMessage () {
		File diretorio = new File("D:\\Saves\\");  
		String [] arquivos = diretorio.list();  
		for(int i=0; i<arquivos.length; i++) {  
			try {
			if (arquivos[i].toString().matches(list.getSelectedValue().toString())) {
				try {
					textArea.setText(readFile("D:\\Saves\\" + arquivos[i].toString()));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			}
			catch (NullPointerException e) {
				
			}
		}  
	}
	
	 private String readFile( String file) throws IOException {

		    BufferedReader reader = new BufferedReader( new FileReader (file));
		    String line  = null;
		    StringBuilder stringBuilder = new StringBuilder();
		    String ls = System.getProperty("line.separator");
		    while( ( line = reader.readLine() ) != null) {
			        stringBuilder.append( line );
			        stringBuilder.append( ls );
		    }
		    
		    return stringBuilder.toString();
	}

	 class OuvinteEdit implements ActionListener{
			public void actionPerformed(ActionEvent ev){
				
				setMessage();
			
			}
		}
	 
	 class OuvinteBotaoOK implements ActionListener{
			public void actionPerformed(ActionEvent ev){
				
				mainCarregaJogo.game.carregaXML("D:\\Saves\\" + list.getSelectedValue().toString());
				
				mainCarregaJogo.aberto = true;
				
				janela.setVisible(false);
				janela.dispose();
				new GUIjogo(mainCarregaJogo);
			}
		}

	 
	/*public static void main (String[] args) {
		new GUIcarregarjogo();
	}*/
	
}
